package com.tsingxiao.codegenerate.api;

import com.tsingxiao.codegenerate.api.interceptor.DemoInterceptor;
import com.tsingxiao.codegenerate.utils.RedisOperator;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author tqccc
 */
@Configuration
@AllArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final RedisOperator redis;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/");
    }

    @Bean
    public DemoInterceptor demoInterceptor() {
        return new DemoInterceptor(redis);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);

        registry.addInterceptor(demoInterceptor())
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/queryPublisher");
    }

}
